package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.service.PurchaseListService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * @author WZhaokang
 * @date 2020/12/29 12:01
 */
@RestController
public class PurchaseListGoodsController {

    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;
    @Autowired
    private PurchaseListService purchaseListService;


    //http://localhost:8080/purchaseListGoods/save?purchaseNumber=JH1605768306735（进货单号前端生成）
    @RequestMapping("/purchaseListGoods/save")
    public ServiceVO save(PurchaseList purchaseList, @RequestParam String purchaseListGoodsStr, HttpSession session){
        //purchaseList:进货单    purchaseListGoodsStr是一个json对象   进货的商品是一个List集合
        //PurchaseList(purchaseListId=null, purchaseNumber=JH1609218411461, amountPaid=25.5, amountPayable=25.5, purchaseDate=2020-12-29, remarks=, state=1, supplierId=1, userId=null, supplierName=null, trueName=null)
        //purchaseListGoodsStr  [{"goodsId":1,"goodsTypeId":10,"goodsCode":"0001","goodsName":"陶华碧老干妈香辣脆油辣椒","goodsModel":"红色装","goodsUnit":"瓶","lastPurchasingPrice":8.5,"price":"8.5","goodsNum":"3","total":25.5}]
       /* System.out.println(purchaseList.getSupplierName());
        System.out.println(purchaseListGoodsStr);*/

        // userId=null, supplierName=null, trueName=null  这三个参数没有

        //插入一个userId，因为少userId ,剩下的那两个不用加，原因是数据库中美哟与这个字段
        User user = (User) session.getAttribute("currentUser"); //得到一个user对象
        Integer userId = user.getUserId();
        purchaseList.setUserId(userId);
        //purchaseListGoodsStr需要加上purchaseList的id
        int res1 = purchaseListService.save(purchaseList,purchaseListGoodsStr);
        
        return new ServiceVO<PurchaseListGoods>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);//进货商品;
    }

    /**
     *
     * @param purchaseNumber 单据号
     * @param supplierId  供应商id
     * @param state  状态码
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    //http://localhost:8080/purchaseListGoods/list
    @RequestMapping("/purchaseListGoods/list")
    public Map<String,List<PurchaseList>> purchaseListGoods(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        Map<String, List<PurchaseList>> map = new HashMap<>();
        //对应的实体就是PurchaseList
        List<PurchaseList> purchaseLists = purchaseListService.list(purchaseNumber,supplierId,state,sTime,eTime);
        map.put("rows",purchaseLists);
        return map;
    }

    //请求URL：http://localhost:8080/purchaseListGoods/goodsList
    //查询进货单商品信息
    @RequestMapping("/purchaseListGoods/goodsList")
    public Map<String,List<PurchaseListGoods>> goodsList(Integer purchaseListId){
        Map map = new HashMap();
        List<PurchaseListGoods>  purchaseListGoodsList =purchaseListGoodsService.goodsList(purchaseListId);
        map.put("rows",purchaseListGoodsList);
        return map;
    }

    //请求URL：http://localhost:8080/purchaseListGoods/delete
    @RequestMapping("/purchaseListGoods/delete")
    public ServiceVO delete(Integer purchaseListId){
        purchaseListGoodsService.delete(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/purchaseListGoods/updateState
    @RequestMapping("/purchaseListGoods/updateState")
    public ServiceVO updateState(Integer purchaseListId){
        purchaseListService.updateState(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    //请求URL：http://localhost:8080/purchaseListGoods/count
    @RequestMapping("/purchaseListGoods/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<PurchaseVO> purchaseListGoodsList = purchaseListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String json = gson.toJson(purchaseListGoodsList);
        return json;
    }
}
